package cn.sciento.farm.devicemanager.api.jsonrpc.impl;


import cn.sciento.farm.devicemanager.api.jsonrpc.AlarmJsonApi;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.dao.entity.DevAlarm;
import cn.sciento.farm.devicemanager.dao.entity.DevAlarmLog;
import cn.sciento.farm.devicemanager.dao.mapper.DevAlarmMapper;
import cn.sciento.farm.devicemanager.dao.model.AlarmForm;
import cn.sciento.farm.devicemanager.dao.model.ResponseMessage;
import cn.sciento.farm.devicemanager.dao.model.filter.AlarmLogFilter;
import cn.sciento.farm.devicemanager.service.AlarmLogService;
import cn.sciento.farm.devicemanager.service.AlarmService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.sciento.farm.devicemanager.common.util.ReturnUtil.getReturnMap;

@Service
@AutoJsonRpcServiceImpl
public class AlarmJsonApiImpl implements AlarmJsonApi {

    @Autowired
    private AlarmService alarmService;

    @Autowired
    AlarmLogService alarmLogService;

    @Override
    public ResponseMessage create(AlarmForm alarmForm) {
        if(StringUtils.isEmpty(alarmForm.getAlarmType())
            || StringUtils.isEmpty(alarmForm.getThreshold())
        || StringUtils.isEmpty(alarmForm.getName())
        || alarmForm.getCollectionId() == 0){
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        int count = alarmService.create(alarmForm);
        return getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage update(AlarmForm alarmForm) {
        if(StringUtils.isEmpty(alarmForm.getAlarmType())
                || StringUtils.isEmpty(alarmForm.getThreshold())
                || StringUtils.isEmpty(alarmForm.getName())){
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        int count = alarmService.updateById(alarmForm);
        return getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage delete(Long id) {
        int count  = alarmService.deleteById(id);
        return getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage list(int areaId) {
        List<AlarmForm> alarmForms = alarmService.getByAreaId(areaId);
        return getReturnMap(ResultConstant.SUCCESS,alarmForms);
    }

    @Override
    public ResponseMessage get(Long id) {
        AlarmForm alarmForm = alarmService.getById(id);
        return getReturnMap(ResultConstant.SUCCESS,alarmForm);
    }

    @Override
    public ResponseMessage getAlarmLog(AlarmLogFilter logFilter) {
        if (logFilter == null || logFilter.getAlarmId() < 1) {
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        IPage<DevAlarmLog> page = alarmLogService.getPage(logFilter);
        return getReturnMap(ResultConstant.SUCCESS,page);
    }
}
